package com.etone.H2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class H2Operator {
	
	private static Connection CONN;
	private static Statement STMT;
	private static String URL = "jdbc:h2:~/bassice";
	private static String USERNAME = "bassice";
	private static String PASSWORD = "bassice";
	
	static {
		new org.h2.Driver();
	}
	
	public static Statement getStmt(){
		return getStmt(H2Operator.URL,H2Operator.USERNAME, H2Operator.PASSWORD);
	}
	
	public static Statement getStmt(String url, String userName, String password){
		
			if(null == STMT){
				try {
					CONN = DriverManager.getConnection(url, userName, password);
					STMT = CONN.createStatement();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return STMT;
	}
	
	public static void main(String ar[]){

		String url = "jdbc:h2:mem:rtmm";
		String userName = "sa";
		String password = "sa";
		String sql = "show tables;";
		String cTable = "Create table test%s(id int primary key, name varchar(22));";
		
		Statement stmt = H2Operator.getStmt(url, userName, password);
		try {
			
			for (int i = 0; i < 5; i++) {
				System.out.println(stmt.execute(String.format(cTable, i)));
			}
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				System.out.println(rs.getString(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
